// merchants/manager/index.js
const app = getApp();
const utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeofCard: 0, //0会员卡，1优惠券
    userInfo: {},
    couponList: [],
    vipcardInfo: {},
    projectList: [],
    screenHeight: 500,
    x: 295,
    y: 146,
    scale: 2
  },

  //获取会员卡
  getVipCard: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardByStaff?uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          let vipcardInfo = res.data.Data;
          if (res.data.Data && res.data.Data.Id > 0) {
            vipcardInfo.EndTime = vipcardInfo.EndTime.substr(0, 10).replace(/-/g, "/");
            vipcardInfo.Discount = vipcardInfo.Discount * 0.1;
            vipcardInfo.Discount = vipcardInfo.Discount.toFixed(1);
            vipcardInfo.CardNo = vipcardInfo.CardNo.replace(/\s/g, '').replace(/(.{4})/g, "$1");
          } else {
            vipcardInfo = {};
            vipcardInfo.EndTime = res.data.ServerTime.substr(0, 10).replace(/-/g, "/");
            vipcardInfo.Title = "VIP会员卡";
            vipcardInfo.Discount = 10;
            vipcardInfo.CardNo = '0000 0000 0000 0000';
          }
          that.setData({
            vipcardInfo: vipcardInfo
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 获取卡项目
  getCardItemListByStaff: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardItemListByStaff?uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({
            projectList: res.data.Data
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  // 我创建的优惠券
  getCounpon: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCouponTypePage',
      data: {
        storeId: app.globalData.storeId,
        type: -1,
        couponTypeName: -1,
        createuid: app.globalData.userInfo.Uid,
        pageSize: 50
      },
      method: 'POST',
      success: function (res) {
        let couponList = res.data.Data;
        let date = new Date();
        for (let i = 0; i < couponList.length; i++) {
          var date2 = new Date(Date.parse(couponList[i].sendendtime.replace(/-/g, "/")));
          if (date2 < date) {
            couponList[i].isExpiretime = 1;
          } else {
            couponList[i].isExpiretime = 0;
          }
          couponList[i].sendstarttime = couponList[i].sendstarttime.substr(0, 10);
          couponList[i].sendendtime = couponList[i].sendendtime.substr(0, 10);
        }
        that.setData({
          couponList: couponList
        })
      }
    })
  },

  // 修改卡项目
  updateCardItem: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../addProject/index?id=' + id,
    })
  },

  // 删除卡项目
  deleteCardItem: function (e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确认删除？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Coupon/DeleteCardItem?id=' + id + '&uid=' + app.globalData.userInfo.Uid,
            success: function (res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '删除成功',
                  duration: 2000
                })
                that.getCardItemListByStaff();
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })

  },
  // 查看已开的卡
  toDetails: function (e) {
    let id = e.currentTarget.dataset.id;
    let cardtype = e.currentTarget.dataset.cardtype;
    wx.navigateTo({
      url: '../projectDetail/index?id=' + id + '&cardtype=' + cardtype,
    })
  },
  // 优惠券会员卡列表的切换
  changeType: function (e) {
    let typeofCard = e.currentTarget.dataset.typeofcard;
    if (typeofCard == 1) {
      this.getCounpon()
    } else {
      this.getVipCard();
      this.getCardItemListByStaff();
    }
    this.setData({
      typeofCard: typeofCard
    })
  },
  //商家查看会员卡详情
  toDetailByStaff: function (e) {
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '../addCoupon/index?coupontypeid=' + coupontypeid + '&type=' + type + '&isEditor=1',
    })
  },
  // 商家查看消费卡详情
  toeditors: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/merchant/merchants/addProject/index?id=' + id + '&isEditor=2',
    })
  },
  // 关闭优惠券
  updateCouponState: function (e) {
    let that = this;
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    let state = e.currentTarget.dataset.state;
    wx.showModal({
      title: '提示',
      content: '作废后不可恢复，是否确认？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Coupon/ChangeCouponTypeState?couponTypeId=' + coupontypeid + '&state=' + state,
            success: function (res) {
              if (res.data.Code == 0) {
                if (!state) {
                  wx.showToast({
                    title: '优惠券已关闭',
                    duration: 2000
                  })
                }
                that.getCounpon();
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },
  
  // 删除优惠券
  deleteCoupon: function (e) {
    let that = this;
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    wx.showModal({
      title: '提示',
      content: '删除后，已发放的该张优惠券失效，是否操作？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Coupon/DeleteCouponType?couponTypeId=' + coupontypeid,
            success: function (res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '删除成功',
                  duration: 1000
                })
                that.getCounpon();
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },
  toDetail: function (e) {
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '../addCoupon/index?coupontypeid=' + coupontypeid + '&type=' + type + '&isEditor=1',
    })
  },

  // 查看核销记录
  toCheckList: function () {
    wx.navigateTo({
      url: '../checkList/index',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var screenHeight = wx.getSystemInfoSync().screenHeight;
    this.setData({
      screenHeight: screenHeight
    })
    if (options.typeofCard) {
      this.getCounpon();
      this.setData({
        typeofCard: options.typeofCard
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      })
    }
    this.getVipCard();
    this.getCardItemListByStaff();
  },

  // 优惠券发放（核销）量
  toUseRecord: function (e) {
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    let listtype = e.currentTarget.dataset.listtype;
    wx.navigateTo({
      url: '/merchant/merchants/sendAndCheckRecords/index?coupontypeid=' + coupontypeid + '&listtype=' + listtype,
    })
  },
  // 查看消费记录
  toRecords: function () {
    wx.navigateTo({
      url: '/merchant/merchants/records/index',
    })
  },
  // 新增项目
  addProject: function () {
    wx.navigateTo({
      url: '/merchant/merchants/addProject/index',
    })
  }
})